import {EventBus} from "./eventbus.js";

export var Router = { //
    routes : {},  //存放路由的
    //添加路由
    addRoute(url, loader){

        this.routes[url] = loader;  //loader是一个函数

        
    },

    //无论如何都必须要加载+layout
    async navigate(){
        var module = await import("./components/+layout.js");
        //从layout文件中引入的模块
        var appDiv = document.getElementById("app");
        if(module && module.Render){
            var html = module.Render();
            if(html){
                appDiv.innerHTML = html;

                if(module.afterRender){
                    module.afterRender();
                }
            }
            EventBus.emit("component:Rendered", module.name || "Unknown");
        }else{
            appDiv.innerHTML = "<h1>组件加载失败</h1>"
        }
    }
}